package com.tc.zigbee.mt;

import java.util.LinkedList;
import java.util.List;

import com.tc.gateway.GroupManager;
import com.tc.gateway.device.GeneralDevice;
import com.tc.gateway.device.Group;
import com.tc.zigbee.entity.Endpoint;
import com.tc.zigbee.entity.ZigbeeGroup;
import com.tc.zigbee.entity.device.ZigbeeDevice;
import com.tc.zigbee.ha.HAConstants;
import com.tc.zigbee.mt.request.RequestFactory;
import com.tc.zigbee.zcl.ZclCommandCreator;
import com.tc.zigbee.zcl.ZclConstants;

public class ZigbeeGroupManager implements GroupManager {
	ZigbeeManager manager;
	
	public ZigbeeGroupManager(ZigbeeManager zm){
		manager = zm;
	}
	public void addDevice(String deviceId, short groupId, String groupName) {
		ZigbeeDevice d = manager.getDevice(deviceId);
		
		Endpoint e = d.getEndpoint();
		byte[] c = ZclCommandCreator.addGroup(groupId, groupName == null?"":groupName,
				(byte) 1);
		MtCommand mc = RequestFactory.MtAfDataRequest((byte) 2, e.getParent()
				.getNwkAddress(),

		e.getEndpointId(), (byte) manager.remoteControl
				.getEndpointId(), (short) 0x0004, (byte) 0, (byte) 0, (byte) 2,
				(byte) c.length, c);
		RequestQueue.offer(mc);
	}
	public void storeScene(String deviceId, short groupId) {
		ZigbeeDevice d = manager.getDevice(deviceId);
		
		Endpoint e = d.getEndpoint();
		
	
		byte[] c = ZclCommandCreator.storeScene((byte)0, groupId, manager.nextSceneId());
		MtCommand mc = RequestFactory.MtAfDataRequest(
				(byte) 2, 
				e.getParent()
				.getNwkAddress(),

				e.getEndpointId(), 
				(byte) manager.remoteControl
				.getEndpointId(), 
				(short) ZclConstants.ZCL_CLUSTER_ID_GEN_SCENES, 
				(byte) 0,
				(byte) 0, 
				(byte) 2,
				(byte) c.length, 
				c);
		RequestQueue.offer(mc);
		}
	public List<GeneralDevice> members(short group) {
		// TODO Auto-generated method stub
		return manager.getGroups().get(group).getMembers();
	}
	public List<GeneralDevice> notmembers(short group) {
		List<GeneralDevice> all = manager.getDevicesProfile(HAConstants.ZCL_HA_PROFILE_ID);
		List<GeneralDevice> ms = manager.getGroups().get(group).getMembers();
		all.removeAll(ms);
		return all;
	}
	public void remove(String deviceId, short groupId) {
		ZigbeeDevice d = manager.getDevice(deviceId);
		
		Endpoint e = d.getEndpoint();
		byte[] c = ZclCommandCreator.removeGroupMember((byte) 1, groupId);
		MtCommand mc = RequestFactory.MtAfDataRequest((byte) 2, e.getParent()
				.getNwkAddress(),

		e.getEndpointId(), (byte) manager.remoteControl
				.getEndpointId(), (short) 0x0004, (byte) 0, (byte) 0, (byte) 2,
				(byte) c.length, c);
		RequestQueue.offer(mc);
		
	}
	public List<Group> getAll() {
		// TODO Auto-generated method stub
		return new LinkedList(manager.groups.values());
	}
	public Group addGroup() {
		ZigbeeGroup g = new ZigbeeGroup();
		g.setId(manager.nextGroupId());
		g.setName("m");
		
		
		manager.getGroups().put(g.getId(), g);
		return g;
	}
	public Group get(Short gid) {
		// TODO Auto-generated method stub
		return manager.groups.get(gid);
	}
		
}
